package com.qiguliuxing.dts.core.manager;

import com.qiguliuxing.dts.common.constant.CommConsts;
import com.qiguliuxing.dts.common.constant.RedisKeyConsts;
import com.qiguliuxing.dts.core.redis.RedisServiceUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存的数据
 */
public class HomeCacheManager {

	/**
	 * 缓存首页数据
	 *
	 * @param data
	 */
	public static void loadData(String cacheKey, Map<String, Object> data) {
		Map<String, Object> cacheData = RedisServiceUtil.get(cacheKey);
		// 有记录，则先丢弃
		if (cacheData != null) {
			cacheData.remove(cacheKey);
		}
		cacheData = new HashMap<>();
		// 深拷贝
		cacheData.putAll(data);
		if (RedisKeyConsts.HOME_CACHE_INDEX.equals(cacheKey)) {// 拷贝后去掉可能含用户独有的信息数据
			cacheData.remove("couponList");
		}
		cacheData.put("isCache", "true");

		// 设置缓存有效期单位 ： 秒
		RedisServiceUtil.set(cacheKey, cacheData, CommConsts.CACHE_EXPIRE_MINUTES * 60L);
	}

	public static Map<String, Object> getCacheData(String cacheKey) {
		return RedisServiceUtil.get(cacheKey);
	}

	/**
	 * 判断缓存中是否有数据
	 *
	 * @return
	 */
	public static boolean hasData(String cacheKey) {
		if (!CommConsts.HOME_ENABLE) {
			return false;
		}
		return RedisServiceUtil.hasKey(cacheKey);
	}

	/**
	 * 清除所有缓存
	 */
	public static void clearAll() {
		RedisServiceUtil.del(RedisKeyConsts.HOME_CACHE_INDEX , RedisKeyConsts.HOME_CACHE_CATALOG);
	}

}
